package com.sdust.controller;

import com.sdust.model.Book;
import com.sdust.utils.jdbcUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/book/books")
public class GetBooksController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Book> books = new ArrayList<>();
        try {
            books = jdbcUtils.getAllBooks();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        req.setAttribute("books", books);
        req.getRequestDispatcher("/book/allBook.jsp").forward(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
